{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import backtrader as bt\n",
    "import datetime\n",
    "import pandas as pd\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create a Stratey\n",
    "class ToyStrategy(bt.Strategy):\n",
    "    '''\n",
    "    Empty strategy is used to study indicators\n",
    "    '''\n",
    "    params = (\n",
    "        ('none', 0),\n",
    "    )\n",
    "    \n",
    "    def log(self, txt):\n",
    "        ''' Logging function for this strategy'''\n",
    "        dt = self.datas[0].datetime.datetime(0)\n",
    "        print('%s, %s' % (dt.isoformat(), txt))\n",
    "        \n",
    "    def __init__(self):\n",
    "        #bt.talib.KAMA(self.data0.close, timeperiod=3)\n",
    "        pass\n",
    "        \n",
    "    def notify_order(self, order):\n",
    "        #print(order)\n",
    "        pass\n",
    "        \n",
    "    def next(self):\n",
    "        self.log('next:{}-{}'.format(self.data0.close[0], len(self)))\n",
    "        l = len(self)\n",
    "        \n",
    "        if 1==l:\n",
    "            self.log('buy1')\n",
    "            self.buy(size=1)\n",
    "        elif 2==l:\n",
    "            self.log('sell2')\n",
    "            self.sell(size=2)\n",
    "        elif 3==l:\n",
    "            self.log('close')\n",
    "            self.close()\n",
    "    \n",
    "    def stop(self):\n",
    "        pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Starting Running\n",
      "2020-06-24T00:11:00, next:10187.709-1\n",
      "2020-06-24T00:11:00, buy1\n",
      "Ref: 1\n",
      "OrdType: 0\n",
      "OrdType: Buy\n",
      "Status: 1\n",
      "Status: Submitted\n",
      "Size: 1\n",
      "Price: None\n",
      "Price Limit: None\n",
      "TrailAmount: None\n",
      "TrailPercent: None\n",
      "ExecType: 0\n",
      "ExecType: Market\n",
      "CommInfo: None\n",
      "End of Session: 737600.9999999999\n",
      "Info: AutoOrderedDict()\n",
      "Broker: None\n",
      "Alive: True\n",
      "Ref: 1\n",
      "OrdType: 0\n",
      "OrdType: Buy\n",
      "Status: 7\n",
      "Status: Margin\n",
      "Size: 1\n",
      "Price: None\n",
      "Price Limit: None\n",
      "TrailAmount: None\n",
      "TrailPercent: None\n",
      "ExecType: 0\n",
      "ExecType: Market\n",
      "CommInfo: None\n",
      "End of Session: 737600.9999999999\n",
      "Info: AutoOrderedDict()\n",
      "Broker: None\n",
      "Alive: False\n",
      "2020-06-24T00:11:30, next:10191.9336-2\n",
      "2020-06-24T00:11:30, sell2\n",
      "Ref: 2\n",
      "OrdType: 1\n",
      "OrdType: Sell\n",
      "Status: 1\n",
      "Status: Submitted\n",
      "Size: -2\n",
      "Price: None\n",
      "Price Limit: None\n",
      "TrailAmount: None\n",
      "TrailPercent: None\n",
      "ExecType: 0\n",
      "ExecType: Market\n",
      "CommInfo: None\n",
      "End of Session: 737600.9999999999\n",
      "Info: AutoOrderedDict()\n",
      "Broker: None\n",
      "Alive: True\n",
      "Ref: 2\n",
      "OrdType: 1\n",
      "OrdType: Sell\n",
      "Status: 2\n",
      "Status: Accepted\n",
      "Size: -2\n",
      "Price: None\n",
      "Price Limit: None\n",
      "TrailAmount: None\n",
      "TrailPercent: None\n",
      "ExecType: 0\n",
      "ExecType: Market\n",
      "CommInfo: None\n",
      "End of Session: 737600.9999999999\n",
      "Info: AutoOrderedDict()\n",
      "Broker: None\n",
      "Alive: True\n",
      "Ref: 2\n",
      "OrdType: 1\n",
      "OrdType: Sell\n",
      "Status: 4\n",
      "Status: Completed\n",
      "Size: -2\n",
      "Price: None\n",
      "Price Limit: None\n",
      "TrailAmount: None\n",
      "TrailPercent: None\n",
      "ExecType: 0\n",
      "ExecType: Market\n",
      "CommInfo: <backtrader.comminfo.CommInfoBase object at 0x10ed7ed50>\n",
      "End of Session: 737600.9999999999\n",
      "Info: AutoOrderedDict()\n",
      "Broker: None\n",
      "Alive: False\n",
      "2020-06-24T00:12:00, next:10192.6617-3\n",
      "2020-06-24T00:12:00, close\n",
      "Ref: 3\n",
      "OrdType: 0\n",
      "OrdType: Buy\n",
      "Status: 1\n",
      "Status: Submitted\n",
      "Size: 2\n",
      "Price: None\n",
      "Price Limit: None\n",
      "TrailAmount: None\n",
      "TrailPercent: None\n",
      "ExecType: 0\n",
      "ExecType: Market\n",
      "CommInfo: None\n",
      "End of Session: 737600.9999999999\n",
      "Info: AutoOrderedDict()\n",
      "Broker: None\n",
      "Alive: True\n",
      "Ref: 3\n",
      "OrdType: 0\n",
      "OrdType: Buy\n",
      "Status: 2\n",
      "Status: Accepted\n",
      "Size: 2\n",
      "Price: None\n",
      "Price Limit: None\n",
      "TrailAmount: None\n",
      "TrailPercent: None\n",
      "ExecType: 0\n",
      "ExecType: Market\n",
      "CommInfo: None\n",
      "End of Session: 737600.9999999999\n",
      "Info: AutoOrderedDict()\n",
      "Broker: None\n",
      "Alive: True\n",
      "Ref: 3\n",
      "OrdType: 0\n",
      "OrdType: Buy\n",
      "Status: 4\n",
      "Status: Completed\n",
      "Size: 2\n",
      "Price: None\n",
      "Price Limit: None\n",
      "TrailAmount: None\n",
      "TrailPercent: None\n",
      "ExecType: 0\n",
      "ExecType: Market\n",
      "CommInfo: <backtrader.comminfo.CommInfoBase object at 0x10ed7ed50>\n",
      "End of Session: 737600.9999999999\n",
      "Info: AutoOrderedDict()\n",
      "Broker: None\n",
      "Alive: False\n",
      "2020-06-24T00:12:30, next:10192.5286-4\n",
      "2020-06-24T00:13:00, next:10192.511-5\n",
      "2020-06-24T00:13:30, next:10190.1938-6\n"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-3-d1f0f6c4d278>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m     17\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Starting Running'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     18\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 19\u001b[0;31m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcerebro\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrun\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     20\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     21\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Finish Running'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/gitrepos/backtrader_bilibili_study/lib/python3.7/site-packages/backtrader/cerebro.py\u001b[0m in \u001b[0;36mrun\u001b[0;34m(self, **kwargs)\u001b[0m\n\u001b[1;32m   1125\u001b[0m             \u001b[0;31m# let's skip process \"spawning\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1126\u001b[0m             \u001b[0;32mfor\u001b[0m \u001b[0miterstrat\u001b[0m \u001b[0;32min\u001b[0m \u001b[0miterstrats\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1127\u001b[0;31m                 \u001b[0mrunstrat\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrunstrategies\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0miterstrat\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   1128\u001b[0m                 \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrunstrats\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mrunstrat\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1129\u001b[0m                 \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_dooptimize\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/gitrepos/backtrader_bilibili_study/lib/python3.7/site-packages/backtrader/cerebro.py\u001b[0m in \u001b[0;36mrunstrategies\u001b[0;34m(self, iterstrat, predata)\u001b[0m\n\u001b[1;32m   1296\u001b[0m                     \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_runnext_old\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mrunstrats\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1297\u001b[0m                 \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1298\u001b[0;31m                     \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_runnext\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mrunstrats\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   1299\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1300\u001b[0m             \u001b[0;32mfor\u001b[0m \u001b[0mstrat\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrunstrats\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/gitrepos/backtrader_bilibili_study/lib/python3.7/site-packages/backtrader/cerebro.py\u001b[0m in \u001b[0;36m_runnext\u001b[0;34m(self, runstrats)\u001b[0m\n\u001b[1;32m   1540\u001b[0m                 \u001b[0mqlapse\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdatetime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdatetime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mutcnow\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0mqstart\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1541\u001b[0m                 \u001b[0md\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdo_qcheck\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnewqcheck\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mqlapse\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtotal_seconds\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1542\u001b[0;31m                 \u001b[0mdrets\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0md\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnext\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mticks\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   1543\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1544\u001b[0m             \u001b[0md0ret\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0many\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdret\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mdret\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mdrets\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/gitrepos/backtrader_bilibili_study/lib/python3.7/site-packages/backtrader/feed.py\u001b[0m in \u001b[0;36mnext\u001b[0;34m(self, datamaster, ticks)\u001b[0m\n\u001b[1;32m    405\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    406\u001b[0m             \u001b[0;31m# not preloaded - request next bar\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 407\u001b[0;31m             \u001b[0mret\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mload\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    408\u001b[0m             \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mret\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    409\u001b[0m                 \u001b[0;31m# if load cannot produce bars - forward the result\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/gitrepos/backtrader_bilibili_study/lib/python3.7/site-packages/backtrader/feed.py\u001b[0m in \u001b[0;36mload\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m    477\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    478\u001b[0m             \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_fromstack\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstash\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 479\u001b[0;31m                 \u001b[0m_loadret\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_load\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    480\u001b[0m                 \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0m_loadret\u001b[0m\u001b[0;34m:\u001b[0m  \u001b[0;31m# no bar use force to make sure in exactbars\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    481\u001b[0m                     \u001b[0;31m# the pointer is undone this covers especially (but not\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/gitrepos/backtrader_bilibili_study/small_toy/simplelivingfeed.py\u001b[0m in \u001b[0;36m_load\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m     33\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0m_load\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     34\u001b[0m         \u001b[0;31m# request data\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 35\u001b[0;31m         \u001b[0mmsg\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_req_data\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     36\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0mmsg\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     37\u001b[0m             \u001b[0;31m# No more data\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/gitrepos/backtrader_bilibili_study/small_toy/simplelivingfeed.py\u001b[0m in \u001b[0;36m_req_data\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m     56\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     57\u001b[0m         \u001b[0;31m# sleep some time\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 58\u001b[0;31m         \u001b[0mtime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msleep\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msleeptime\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     59\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     60\u001b[0m         \u001b[0mmsg\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdict\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "cerebro = bt.Cerebro(oldtrades=False, stdstats=False)\n",
    "\n",
    "from simplelivingfeed import SimpleLivingData\n",
    "from autoguibroker import AutoGuiBroker\n",
    "\n",
    "feed = SimpleLivingData('ixic', timeframe=bt.TimeFrame.Seconds, compression=30)\n",
    "cerebro.adddata(feed, name='simple living')\n",
    "#cerebro.resampledata(feed, name='cy_weekly', timeframe=bt.TimeFrame.Minutes, compression=1)\n",
    "\n",
    "cerebro.addstrategy(ToyStrategy)\n",
    "\n",
    "#cerebro.broker = AutoGuiBroker()\n",
    "\n",
    "# init cash\n",
    "cerebro.broker.setcash(10000.0)\n",
    "\n",
    "print('Starting Running')\n",
    "\n",
    "result = cerebro.run()\n",
    "\n",
    "print('Finish Running')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
